//
// Copyright 2022 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

// Selector '.mdc-*' should only be used in this project.
// stylelint-disable selector-class-pattern --
// Internal styling for Menu MDC component.

@use 'sass:map';
@use 'sass:meta';
@use '@material/elevation/elevation-theme';
@use '@material/list/list-theme';
@use '@material/shape/shape';
@use '@material/theme/keys';
@use '@material/theme/map-ext';
@use '@material/theme/theme';
@use '@material/theme/validate';
@use '@material/tokens/resolvers';

$custom-property-prefix: 'menu';

$light-theme: (
  cascading-menu-indicator-icon-color: null,
  cascading-menu-indicator-icon-size: null,
  container-color: null,
  container-elevation: null,
  container-shadow-color: null,
  container-shape: null,
  container-surface-tint-layer-color: null,
  divider-color: null,
  divider-height: null,
  list-item-container-height: null,
  list-item-disabled-label-text-color: null,
  list-item-disabled-label-text-opacity: null,
  list-item-focus-label-text-color: null,
  list-item-focus-state-layer-color: null,
  list-item-focus-state-layer-opacity: null,
  list-item-hover-label-text-color: null,
  list-item-hover-state-layer-color: null,
  list-item-hover-state-layer-opacity: null,
  list-item-label-text-color: null,
  list-item-label-text-font: null,
  list-item-label-text-line-height: null,
  list-item-label-text-size: null,
  list-item-label-text-tracking: null,
  list-item-label-text-type: null,
  list-item-label-text-weight: null,
  list-item-pressed-label-text-color: null,
  list-item-pressed-state-layer-color: null,
  list-item-pressed-state-layer-opacity: null,
  list-item-selected-container-color: null,
  list-item-with-leading-icon-disabled-leading-icon-color: null,
  list-item-with-leading-icon-disabled-leading-icon-opacity: null,
  list-item-with-leading-icon-focus-icon-color: null,
  list-item-with-leading-icon-hover-icon-color: null,
  list-item-with-leading-icon-leading-icon-color: null,
  list-item-with-leading-icon-leading-icon-size: null,
);

@mixin theme($theme, $resolvers: resolvers.$material) {
  $theme: validate.theme($light-theme, $theme);
  $theme: _resolve-theme-elevation($theme, $resolvers: $resolvers);
  @include keys.declare-custom-properties(
    $theme,
    $prefix: $custom-property-prefix
  );
}

@mixin theme-styles(
  $theme,
  $resolvers: resolvers.$material,
  $require-all: true
) {
  $theme: validate.theme-styles(
    $light-theme,
    $theme,
    $require-all: $require-all
  );
  $theme: _resolve-theme-elevation($theme, $resolvers: $resolvers);
  $theme: keys.create-theme-properties(
    $theme,
    $prefix: $custom-property-prefix
  );

  @include theme.property(background-color, map.get($theme, 'container-color'));
  @include shape.radius(map.get($theme, 'container-shape'));
  @include elevation-theme.overlay-container-color(
    map.get($theme, 'container-surface-tint-layer-color')
  );
  @if map.get($theme, 'container-elevation') {
    @include elevation-theme.theme-styles(
      map.get($theme, 'container-elevation')
    );
  }

  .mdc-menu-item--selected {
    @include theme.property(
      background-color,
      map.get($theme, 'list-item-selected-container-color')
    );
  }

  .mdc-menu-item__submenu-indicator {
    @include theme.property(
      color,
      map.get($theme, 'cascading-menu-indicator-icon-color')
    );
    @include theme.property(
      font-size,
      map.get($theme, 'cascading-menu-indicator-icon-size')
    );
    @include theme.property(
      width,
      map.get($theme, 'cascading-menu-indicator-icon-size')
    );
    @include theme.property(
      height,
      map.get($theme, 'cascading-menu-indicator-icon-size')
    );
  }

  .mdc-list {
    $list-theme: _rename-list-theme($theme);
    $list-theme: map-ext.pick(
      $list-theme,
      map.keys(list-theme.$light-theme)...
    );
    // Make the list item container color transparent to allow the elevation
    // overlay and menu container color to be visible.
    $list-theme: map.merge(
      $list-theme,
      (
        'list-item-container-color': transparent,
      )
    );
    @include list-theme.theme($list-theme);
  }
}

@function _rename-list-theme($theme) {
  $theme: map-ext.rename-keys(
    $theme,
    (
      'list-item-with-leading-icon-disabled-leading-icon-color':
        'list-item-disabled-leading-icon-color',
      'list-item-with-leading-icon-disabled-leading-icon-opacity':
        'list-item-disabled-leading-icon-opacity',
      'list-item-with-leading-icon-hover-icon-color':
        'list-item-hover-leading-icon-color',
      'list-item-with-leading-icon-leading-icon-color':
        'list-item-leading-icon-color',
      'list-item-with-leading-icon-leading-icon-size':
        'list-item-leading-icon-size',
      'cascading-menu-indicator-icon-size': 'list-item-trailing-icon-size',
    )
  );
  @return $theme;
}

@function _resolve-theme-elevation($theme, $resolvers) {
  $elevation-resolver: map.get($resolvers, elevation);
  $shadow-color: map.get($theme, 'container-shadow-color');
  @if $elevation-resolver == null or $shadow-color == null {
    @return $theme;
  }

  $elevation-keys: ('container-elevation');

  @each $key in $elevation-keys {
    $elevation: map.get($theme, $key);
    @if $elevation != null {
      $resolved-value: meta.call(
        $elevation-resolver,
        $elevation: $elevation,
        $shadow-color: $shadow-color
      );
      $theme: map.set($theme, $key, $resolved-value);
    }
  }
  @return $theme;
}
